﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanSlide.UIElement.Table.Structure.Element;
using Windows.Data.Xml.Dom;
using Windows.Storage.Streams;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace BoolanElement.UI.Table
{
    public class BooTableData
    {
        public List<List<BooTableElementUnit>> Data;

        public BooTableData(BooTableElement element)
        {
            Data = element.Units;
        }

        public void SetData(BooTableDataUnit data,int row,int column)
        {
            int rowCount = Data.Count;
            int columnCount = Data.First().Count;
            if (row < 0 || row > rowCount - 1)
                return;
            if (column < 0 || column > columnCount - 1)
                return;
            List<BooTableElementUnit> rowData=Data.ElementAt(row);
            rowData[column].Data = data;
        }

        public XElement Save(BooTableElement tableElement)
        {
            int rowCount = Data.Count;
            int columnCount = Data.First().Count;
            XElement element = new XElement("Data");
            element.SetAttributeValue("Row", rowCount.ToString());
            element.SetAttributeValue("Column", columnCount.ToString());
            for (int i = 0; i < rowCount; ++i)
            {
                for (int j = 0; j < columnCount; ++j)
                {
                    XElement dataNode = new XElement("DataUnit");
                    dataNode.SetAttributeValue("Row",i.ToString());
                    dataNode.SetAttributeValue("Column", j.ToString());
                    element.Add(dataNode);
                    dataNode.Value = RichTextData.RtfToBase64(GetUnit(i, j).DataString);
                }
            }

            return element;
        }

        public async Task<bool> Load(BooTableElement ele,XElement element)
        {
            int row = Convert.ToInt32(element.Attribute("Row").Value);
            int column = Convert.ToInt32(element.Attribute("Column").Value);
            for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j < column; ++j)
                {
                    XElement[] node = element.Elements("DataUnit").Where(result => result.Attribute("Row").Value == i.ToString() && result.Attribute("Column").Value == j.ToString()).ToArray();
                    if (node.Length>0)
                    {
                        string data=node[0].Value;
                        SetData(new BooTableDataUnit(RichTextData.Base64ToRtf(data)), i, j);
                    }
                }
            }
            return true;
        }

        public String GetData(int row,int column)
        {
            return GetStringData(row,column);
        }

        public void SetDataDocument(int row, int column, RichEditBox box)
        {
            if (GetUnit(row, column).DataString != null)
            {
                box.Document.SetText(TextSetOptions.FormatRtf | TextSetOptions.ApplyRtfDocumentDefaults, GetUnit(row, column).DataString);
            }
        }

        public BooTableDataUnit GetUnit(int row, int column)
        {
            return Data.ElementAt(row).ElementAt(column).Data;
        }

        public void SetRowData(int row,List<BooTableDataUnit> rowData)
        {
            if (rowData == null)
                return;
            int count = Data.ElementAt(row).Count > rowData.Count ? rowData.Count : Data.ElementAt(row).Count;
            for (int i = 0; i < count; ++i)
            {
                Data.ElementAt(row)[i].Data.Load(rowData.ElementAt(i));
            }
        }

        public void SetColumnData(int column,List<BooTableDataUnit> columnData)
        {
            if (columnData == null)
                return;
            int count = Data.Count > columnData.Count ? columnData.Count : Data.Count;
            for (int i = 0; i < count; ++i)
            {
                Data.ElementAt(i).ElementAt(column).Data.Load(columnData.ElementAt(i));
            }
        }

        public List<BooTableDataUnit> GetRowData(int row)
        {
            List<BooTableDataUnit> units = new List<BooTableDataUnit>();
            foreach (var item in Data.ElementAt(row))
            {
                units.Add(item.Data);
            }
            return units;
        }

        public List<BooTableDataUnit> GetColumnData(int row)
        {
            List<BooTableDataUnit> ret = new List<BooTableDataUnit>();
            foreach (var rowdata in Data)
            {
                ret.Add(rowdata.ElementAt(row).Data);
            }
            return ret;
        }

        private String GetStringData(int row,int cloumn)
        {
            return Data.ElementAt(row).ElementAt(cloumn).Data.DataString;
        }
    }
}
